/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/23/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define ChiTietPhieuNhapHangDao class. This class inherit from  ChiTietPhieuNhapHangDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class ChiTietPhieuNhapHangDao : ChiTietPhieuNhapHangDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public ChiTietPhieuNhapHangDao() { }
        static ChiTietPhieuNhapHangDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(ChiTietPhieuNhapHangDao).FullName);
        }
        public static List<ChiTietPhieuNhapHangResult> GetByMaPhieuNhapHang(Int64 maPhieuNhaphang)
        {
            String methodName = "GetByMaPhieuNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietPhieuNhapHangResult> listChiTietPhieuNhapHang = new List<ChiTietPhieuNhapHangResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetChiTietPhieuNhapHangByMaPhieuNhapHang");
                //Add value "MaPhieuNhapHang"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuNhapHang", DbType.Int64, maPhieuNhaphang);
                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listChiTietPhieuNhapHang.Add(GetChiTietPhieuNhapHangResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listChiTietPhieuNhapHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        public static bool DeleteByMaPhieuNhap(Int64 MaPhieuNhapHang)
        {
            String methodName = "DeleteByMaPhieuNhap";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteChiTietPhieuNhapHangByMaPhieuNhap");
                //Add value "MaPhieuNhapHang"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuNhapHang", DbType.Int64, MaPhieuNhapHang);
              
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
        protected static ChiTietPhieuNhapHangDetail GetChiTietPhieuNhapHangDetailByDataReader(IDataReader reader)
        {
            String methodName = "GetChiTietPhieuNhapHangDetailByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                ChiTietPhieuNhapHangDetail result = new ChiTietPhieuNhapHangDetail();
                //read MaPhieuNhapHang field data.
                result.LoaiMatHang = ManagerDao.GetstringDataFromDataReader(reader, "LoaiMatHang");

                //read MaMatHang field data.
                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");
                result.MaPhuTung = ManagerDao.GetstringDataFromDataReader(reader, "MaPhuTung");
                result.MaXe = ManagerDao.GetintDataFromDataReader(reader, "MaXe");
                result.TenMatHang = ManagerDao.GetstringDataFromDataReader(reader, "TenMatHang");
                result.NuocSX = ManagerDao.GetstringDataFromDataReader(reader, "NuocSanXuat");
                result.Hieu = ManagerDao.GetstringDataFromDataReader(reader, "Hieu");

                //read MaDonViTinh field data.
                result.DonViTinh = ManagerDao.GetstringDataFromDataReader(reader, "DonViTinh");

                //read SoLuongNhap field data.
                result.SoLuongNhap = ManagerDao.GetintDataFromDataReader(reader, "SoLuongNhap");
                //read GiaNhap field data.
                result.GiaNhap = ManagerDao.GetdoubleDataFromDataReader(reader, "GiaNhap");
                result.CurencyCode = ManagerDao.GetstringDataFromDataReader(reader, "CurencyCode");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static List<ChiTietPhieuNhapHangDetail> GetChiTietPhieuNhapHangDetailByMaPhieuNhapHang(Int64 maPhieuNhaphang)
        {
            String methodName = "GetChiTietPhieuNhapHangDetailByMaPhieuNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietPhieuNhapHangDetail> listChiTietPhieuNhapHang = new List<ChiTietPhieuNhapHangDetail>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetChiTietPhieuNhapHangDetail");
                //Add value "MaPhieuNhapHang"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuNhapHang", DbType.Int64, maPhieuNhaphang);
                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listChiTietPhieuNhapHang.Add(GetChiTietPhieuNhapHangDetailByDataReader(reader));
                    }
                    reader.Close();
                }

                return listChiTietPhieuNhapHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
    }
}
        #endregion